import { defineStore } from 'pinia';
import { ref } from "vue"
import { getStorage, redirectTo } from '@/common/utils';

export const useUserStore = defineStore('user', () => {
	const isLogin = ref(getStorage("token")?.length > 0);

	const setLogin = (value : boolean) => {
		if (value === false) {
			uni.removeStorageSync('token')
		}
		isLogin.value = value
	}

	const checkLogin = () => {
		return new Promise((resolve) => {
			if (isLogin.value) {
				resolve(true)
				return
			}
			uni.showModal({
				title: '提示',
				content: '为了您的使用体验，请先登录',
				success: async function (res) {
					if (res.confirm) {
						redirectTo("/pages/login/login")
						return
					}
				}
			})
		})
	}

	return { isLogin, checkLogin, setLogin };
});